package biz;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DbComBiz {
	Configuration configuration = null;
	SessionFactory sessionFactory = null;
	Session session = null;
	Transaction transaction = null;
	
	public String getMaxId(String type) {

		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		Integer imax=0;

		Query query = session.createQuery("select u.maxid from TMaxId as u where u.type ='"
				+ type + "'");
		Iterator iter = query.iterate();
		while(iter.hasNext()){
			String tmp = iter.next().toString();
			imax= Integer.parseInt(tmp);
			imax++;
		}
		boolean flag=true;
		String maxid = imax.toString();
		while(flag) {
			if(maxid.length() < 8) {
				maxid = "0" + maxid;
			}else{
				flag = false;
			}
		}
		session.close();
		return maxid;
	}
	
	public void updMaxId(String type, Integer imax) {

		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		Query query = session.createQuery("update TMaxId as u set u.maxid = " + imax + " where u.type ='"
				+ type + "'");
		query.executeUpdate();
		transaction.commit();
		session.close();
	}
	
	public boolean getUserId(String uname) {

		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		Integer imax=0;

		Query query = session.createQuery("select u.id from TUserInfo as u where u.name ='"
				+ uname + "'");
		Iterator iter = query.iterate();
		String tmp = "";
		while(iter.hasNext()){
			tmp = iter.next().toString();
		}
		session.close();
		if(tmp == null) {
			return false;
		}else if("".equals(tmp)){
			return false;
		}else{
			return true;
		}
	}
	
	public String getUserName(String uid) {

		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();

		Query query = session.createQuery("select u.name from TUserInfo as u where u.id ='"
				+ uid + "'");
		Iterator iter = query.iterate();
		String tmp = "";
		while(iter.hasNext()){
			tmp = iter.next().toString();
		}
		session.close();
		return tmp;
	}
	
	public String getClientName(String cid) {

		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();

		Query query = session.createQuery("select u.name from TClientInfo as u where u.id ='"
				+ cid + "'");
		Iterator iter = query.iterate();
		String tmp = "";
		while(iter.hasNext()){
			tmp = iter.next().toString();
		}
		session.close();
		return tmp;
	}
	
	public List getUserList() {

		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();

		Query query = session.createQuery(" from TUserInfo as u order by u.name");
		List list = query.list();
		
		return list;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
